package com.webex.ta.hydra.plugin;

import com.webex.ta.hydra.util.MiscUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-28
 * Time: 10:40:02
 */
public class Plugin {
    private static final String PLUGIN_PFX = "plugin.";
    private static final String NAME = PLUGIN_PFX + "name";
    private static final String CLASS = PLUGIN_PFX + "class";
    private static final String AUTHOR = PLUGIN_PFX + "author";
    private static final String JARS = PLUGIN_PFX + "jars";
    private static final String VERSION = PLUGIN_PFX + "version";
    private static final String DESCRIPTION = PLUGIN_PFX + "description";

    private String name;
    private String clazz;
    private String author;
    private String version;
    private String description;
    private String category;
    private PluginJar jar;
    private boolean activated = false;
    private List<Branch> branches = new ArrayList<Branch>();

    public Plugin() {
        jar = null;
    }

    public Plugin(PluginJar jar) throws IOException {
        this.jar = jar;
        InputStream in = jar.getClassLoader().getResourceAsStream("plugin.meta");
        Properties props = new Properties();
        props.load(in);
        name = props.getProperty(NAME);
        clazz = props.getProperty(CLASS);
        author = props.getProperty(AUTHOR);
        version = props.getProperty(VERSION);
        description = props.getProperty(DESCRIPTION);

//        String jars = props.getProperty(JARS);
//        StringTokenizer st = new StringTokenizer(jars);
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public String getClazz() {
        return clazz;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getAuthor() {
        return author;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getVersion() {
        return version;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public PluginJar getPluginJar() {
        return jar;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setActivated(boolean activated) {
        this.activated = activated;
    }

    public boolean isActivated() {
        return activated;
    }

    public boolean hasBranch() {
        return branches.size() > 0;
    }

    public void clearBranches() {
        branches.clear();
    }

    public Branch getLatestBranch() {
        if (hasBranch()) {
            Collections.sort(branches, new VersionCompare());
            return branches.get(0);
        } else
            return null;
    }

    public String getLatestVersion() {
        Collections.sort(branches, new VersionCompare());
        if (branches.size() > 0)
            return branches.get(0).getVersion();
        else if (version != null)
            return version;
        return "";
    }

    public void addBranch(Branch branch) {
        branches.add(branch);
    }

    public static class Branch {
        private String version;
        private String date;
        private int size;
        private String download;

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getDownload() {
            return download;
        }

        public void setDownload(String download) {
            this.download = download;
        }

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }
    }

    private class VersionCompare implements Comparator<Branch> {
        public int compare(Branch b1, Branch b2) {
            return MiscUtils.compareStrings(b1.getVersion(), b2.getVersion(), true);
        }
    }
}
